// pages/countdown/countdown.js
var util = require('../../utils/util.js')
var http = require('../../utils/http.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: '',
    rank: '',
    realName: '',
    userLogo: '',
    rankType: 0,
    users: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //上周的开始时间
    var lastBegin = that.getTime(7);
    console.log(lastBegin);
    //上周的结束时间
    var lastEnd = that.getTime(1);
    that.setData({
      lastBegin,
      lastEnd
    })
    console.log(lastEnd);
    var rankType = that.data.rankType;
    const data = {
      rankType: rankType,
    }
    //获取排行榜页面数据
    http.getRank(data).then( (res) => {
      var num = res.userRank.num;
      var rank = res.userRank.rank + 1;
      var realName = res.userRank.realName;
      var users = res.users;
      for( var i = 0; i < users.length; i++) {
        users[i].realName = unescape(users[i].realName);
      }
      var userLogo = wx.getStorageSync('touxiang');
      if( userLogo) {
        that.setData({
          userLogo,
        })
      } else {
        var userLogo = res.userRank.userLogo;
        that.setData({
          userLogo,
        })
      }
      that.setData({
        num,
        rank,
        realName,
        users
      })
    })
  },
  getTime: function(n) {
      var now=new Date();
      var year=now.getFullYear();
      //因为月份是从0开始的,所以获取这个月的月份数要加1才行
      var month=now.getMonth()+1;
      var date=now.getDate();
      var day=now.getDay();
      // console.log(date);
      //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
      if(day!==0){
      n=n+(day-1);
      }
      else{
      n=n+day;
      }
      if(day){
      //这个判断是为了解决跨年的问题
      if(month>1){
      month=month;
      }
      //这个判断是为了解决跨年的问题,月份是从0开始的
      else{
      year=year-1;
      month=12;
      }
      }
      now.setDate(now.getDate()-n);
      year=now.getFullYear();
      month=now.getMonth()+1;
      date=now.getDate();
      // console.log(n);
      var s=(month<10?('0'+month):month)+'.'+(date<10?('0'+date):date);
      return s;
  },
  // 切换排行数据
  changeBank: function() {
    var that = this;
    var rankType = that.data.rankType === 0 ? 1:0;
    that.setData({
      rankType,
    })
    const data = {
      rankType: rankType,
    }
    http.getRank(data).then( (res) => {
      var num = res.userRank.num;
      var rank = res.userRank.rank + 1;
      var realName = res.userRank.realName;
      var users = res.users;
      for( var i = 0; i < users.length; i++) {
        users[i].realName = unescape(users[i].realName);
      }
      var userLogo = wx.getStorageSync('touxiang');
      if( userLogo) {
        that.setData({
          userLogo,
        })
      } else {
        var userLogo = res.userRank.userLogo;
        that.setData({
          userLogo,
        })
      }
      that.setData({
        num,
        rank,
        realName,
        users
      })
    })
  },
  back:function () {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})